﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace rbdesign.Model
{
    class ModelUser
    {
        public static List<User> fGetUsersDetails()
        {
            try
            {
                DataTable dt = ModelCRUD.Select("SELECT id,username,password,email,birthdate,sex,interests FROM tblUser");

                List<User> listUser = new List<User>();

                foreach (DataRow drRow in dt.Rows)
                {
                    User newUser = new User(int.Parse(drRow["id"].ToString()), drRow["username"].ToString(), drRow["password"].ToString(), drRow["email"].ToString(), drRow["birthdate"].ToString(), drRow["sex"].ToString()[0], drRow["interests"].ToString());
                    listUser.Add(newUser);
                }

                return listUser;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static List<string> fGetUsernamesList()
        {
            try
            {
                DataTable dt = ModelCRUD.Select("SELECT username FROM tblUser");

                List<string> listUserNames = new List<string>();

                foreach (DataRow drRow in dt.Rows)
                {
                    listUserNames.Add((string)drRow["username"]);
                }

                return listUserNames;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static User fGetUserDetailsSingle(string sUsername)
        {
            try
            {
                DataTable dt = ModelCRUD.Select("SELECT id,username,password,email,birthdate,sex,interests FROM tblUser WHERE username='" + sUsername +"';");

                User newUser = null;

                foreach (DataRow drRow in dt.Rows)
                {
                    newUser = new User(int.Parse(drRow["id"].ToString()), drRow["username"].ToString(), drRow["password"].ToString(), drRow["email"].ToString(), drRow["birthdate"].ToString(), drRow["sex"].ToString()[0], drRow["interests"].ToString());
                }

                return newUser;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static bool fAddUser(string _username, string _password, string _email, string _birthdate, char _sex, string _interests)
        {
            try
            {
                ModelCRUD.InsertUpdateDelete("INSERT INTO tblUser(username,password,email,birthdate,sex,interests) Values('"+_username+"','"+_password+"','"+_email+"','"+_birthdate+"','"+_sex+"','"+_interests+"')");

                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static bool fEditUser(string _username, string _password, string _email, string _birthdate, char _sex, string _interests)
        {
            try
            {
                ModelCRUD.InsertUpdateDelete("UPDATE tblUser SET password='" + _password + "',email='" + _email + "',birthdate='" + _birthdate + "',sex='" + _sex + "',interests='" + _interests + "' WHERE username='" + _username + "';");

                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static bool fDeleteUser(string _username)
        {
            try
            {
                ModelCRUD.InsertUpdateDelete("DELETE FROM tblUser WHERE username='" + _username + "';");

                return true;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
